package com.surfilter.massdata.spark.util;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.TreeMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.surfilter.massdata.spark.model.DnsStatConsts;
import com.surfilter.massdata.spark.model.StructDomain;

public class DomainDatabase implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	private static final Log log = LogFactory.getLog(DomainDatabase.class);
	private static DomainDatabase _db = null;
	public final TreeMap<String,StructDomain> domainExtensionMap = new TreeMap<String,StructDomain>();

	private DomainDatabase() {
		ResultSet rs = null;
		String sql = "select top_domain,site_name,registrar_name,record_state,cn_domain_name,license_key,is_black,parent_industry_type from br2005";
		Statement stmt = null;
		Connection conn = null;
		try {
			conn = JdbcUtil.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
            int i =0;
			while (rs.next()) {
				String top_domain = rs.getString("TOP_DOMAIN");
				StructDomain struct = resultToStruDomain(rs);
				if (struct == null) {
					continue;
				}
                i++;
				synchronized (domainExtensionMap) {
					domainExtensionMap.put(top_domain,struct);
				}
			}
            log.info("DomainDatabase size is " + i);
        } catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		} finally {
			JdbcUtil.close(rs, stmt, conn);
		}
	}

	public static DomainDatabase getInstance() {
		if (_db == null)
			_db = new DomainDatabase();
		return _db;
	}

	public StructDomain get(String top_domain) {
		try {
			StructDomain struct = domainExtensionMap.get(top_domain);
			return struct;
		}catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
			return null;
		}
	}

	private StructDomain resultToStruDomain(ResultSet result) {
		StructDomain struct = null;
		try {
			struct = new StructDomain(result.getString("TOP_DOMAIN"));
			
			String value = result.getString("SITE_NAME");
			struct.set(DnsStatConsts.DomainExField.SITE_NAME, value);
			
			value = result.getString("REGISTRAR_NAME");
			struct.set(DnsStatConsts.DomainExField.REGISTRAR_NAME, value);
			
			value = result.getString("RECORD_STATE");
			struct.set(DnsStatConsts.DomainExField.RECORD_STATE, value);
			
			value = result.getString("CN_DOMAIN_NAME");
			struct.set(DnsStatConsts.DomainExField.CN_DOMAIN_NAME, value);
			
			value = result.getString("LICENSE_KEY");
			struct.set(DnsStatConsts.DomainExField.LICENSE_KEY, value);
			
			value = result.getString("IS_BLACK");
			struct.set(DnsStatConsts.DomainExField.IS_BLACK, value);
			
			value = result.getString("PARENT_INDUSTRY_TYPE");
			struct.set(DnsStatConsts.DomainExField.PARENT_INDUSTRY_TYPE, value);
		} catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return struct;
	}
}

